package com.john.booter.module.news.service.impl;

import java.io.IOException;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.john.booter.config.BooterSettings;
import com.john.booter.domain.entity.File;
import com.john.booter.domain.entity.News;
import com.john.booter.exception.FileOptionException;
import com.john.booter.module.file.dao.FileDao;
import com.john.booter.module.file.service.FileService;
import com.john.booter.module.news.dao.NewsDao;
import com.john.booter.module.news.service.NewsService;
import com.john.booter.pagination.DataTable;
import com.john.booter.pagination.Pager;


@Service
public class NewsServiceImpl implements NewsService {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private NewsDao dao;
	
	@Autowired
	private FileDao fileDao;
	
	@Autowired
	private BooterSettings setting; 
	
	@Autowired
	private FileService fileService;

	@Transactional
	@Override
	public void add(News news) {
		List<File> files = fileService.saveEditorContentFile(news.getContent());
		news.setFiles(files);
		dao.save(news);
	}

	@Transactional
	@Override
	public void delete(String id) {
		dao.delete(id);
	}

	@Transactional
	@Override
	public void update(News news) {
		News old = findById(news.getId());
		List<File> files = fileService.updateEditorContentFile(news.getContent(), old.getContent());
		news.setFiles(files);
		dao.update(news);
	}

	@Override
	public List<News> findAll() {
		return dao.findAll();
	}

	@Override
	public News findById(String id) {
		return dao.findById(id);
	}

	@Override
	public DataTable<News> findDataTable(Pager pg, String title) {
		return dao.findDataTable(pg, title);
	}

}
